﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MCServerHelper.Properties;

namespace MCServerHelper.Services
{
    public class MotdService : IMinecraftService
    {
        private MinecraftServer _server;
        private const string SET_MOTD_COMMAND = "SETMOTD ";
        private string _motd = string.Empty;

        public void Initialize(MinecraftServer server)
        {
            _server = server;
        }

        public void ProcessOutput(string output)
        {
            if (Settings.Default.AllowServerCommands)
            {
                int index = output.IndexOf(SET_MOTD_COMMAND);
                int commandEnd = index + SET_MOTD_COMMAND.Length;
                if (index >= 0)
                {
                    _motd = output.Substring(commandEnd, output.Length - commandEnd);
                }
            }
        }

        public void Dispose()
        {
        }

        public void ServerStarted()
        {
        }

        public void ServerStopped()
        {
        }

        public void PlayerConnected(string name, string ip)
        {
            if (!string.IsNullOrEmpty(_motd))
            {
                _server.WriteInput(string.Format(Command.TellPlayerMessage, name, _motd));
            }
        }
    }
}
